#include <interfacelib/frame/windowmain.h>
#include "frame/framemanager.h"
#include <utillib/util/util.h>
#include <utillib/util/ExceptionHandler.h>

utillib::util::ExceptionHandler g_exceptionHandler;

#define	FRAME_PER_MILLISEC	33

bool updateFrame()
{
	static DWORD previousTickCount = GetTickCount();
	DWORD checkTime = utillib::util::Util::getCheckTime( previousTickCount );
	if ( checkTime < FRAME_PER_MILLISEC )
	{
		previousTickCount += FRAME_PER_MILLISEC;
		FrameManager::getInstance()->updateFrame();
	}
	return ( true );
}

bool drawFrame()
{
	FrameManager::getInstance()->drawFrame();
	return ( true );
}

int messageProcedure( HWND windowHandle,  MSG *msg )
{
	if ( msg->message == WM_QUIT ) return ( APP_EXIT );

	FrameManager::getInstance()->preTranslateMessage( msg );
	return ( 0 );
}

int PASCAL WinMain( HINSTANCE instance, HINSTANCE prevInstance, LPSTR commandLine, int commandShow )
{
	g_exceptionHandler.initialize();
	interfacelib::Frame::WindowMain windowMain( messageProcedure, drawFrame, updateFrame );

	windowMain.initialize( instance, "TestClient", 800, 600, true );

	FrameManager::getInstance()->initialize( instance, windowMain.getHwnd() );

	FrameManager::getInstance()->createFrame();

	FrameManager::getInstance()->changeFrame( FrameManager::FRAME_LOBBY );

	windowMain.run();

	FrameManager::getInstance()->release();
	return ( 0 );
}